<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="coinfaces">

        <title>Python except语法误解</title>

    <link rel="stylesheet" href="../../theme/css/style.css">
    <link rel="stylesheet" href="../../theme/css/pygments.css">



</head>

<body>
    <div class="content">
<h1>Python except语法误解</h1>    <p>
        under
            <a href="../../tags/python/">python</a>
    </p>
    <p>
        in <a href="../../categories/tech/">tech</a>
    </p>
    <p>Published: 2016-10-14</p>


    <p>Python里很多情况下<tt class="docutils literal">a, b, c</tt>等价于<tt class="docutils literal">(a, b, c)</tt>，比如表示和分解tuple，以下四种写法等价：</p>
<div class="highlight"><pre><span></span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span>
<span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span>
<span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
</pre></div>
<p>在<tt class="docutils literal">from ... import ...</tt>语句中：</p>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">os</span> <span class="kn">import</span> <span class="n">chmod</span><span class="p">,</span> <span class="n">chown</span>
<span class="kn">from</span> <span class="nn">os</span> <span class="kn">import</span> <span class="p">(</span><span class="n">unlink</span><span class="p">,</span> <span class="n">rmdir</span><span class="p">)</span>
</pre></div>
<p>但<tt class="docutils literal">except</tt>子句确实是个例外，<tt class="docutils literal">except a, b</tt>的意思是<tt class="docutils literal">except a as b</tt>，是为了向后兼容而保留的语法，这种语法已被Python 3禁用，如果想在一个子句里捕获两类异常，应该写成<tt class="docutils literal">except (a, b)</tt>。</p>
<p>如果不小心遗忘了括号，可能会引起莫名奇妙的错误，如果用Python 2运行下面这段代码：</p>
<div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ErrorA</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="k">class</span> <span class="nc">ErrorB</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="k">try</span><span class="p">:</span>
    <span class="k">raise</span> <span class="n">ErrorA</span><span class="p">()</span>
<span class="k">except</span> <span class="n">ErrorA</span><span class="p">,</span> <span class="n">ErrorB</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Caught ErrorA or ErrorB&quot;</span><span class="p">)</span>

<span class="k">try</span><span class="p">:</span>
    <span class="k">raise</span> <span class="n">ErrorB</span><span class="p">()</span>
<span class="k">except</span> <span class="n">ErrorB</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Caught ErrorB&quot;</span><span class="p">)</span>
</pre></div>
<p>会被提示<tt class="docutils literal">TypeError: 'ErrorA' object is not callable</tt>，因为<tt class="docutils literal">ErrorB</tt>被<tt class="docutils literal">ErrorA</tt>的实例对象覆盖了。</p>
<p>（完）</p>

    </div>
<nav class="nav sidebar">
    <div class="siteinfo">
        <div class="sitename"><a href="../..">coinfaces</a></div>
        <div class="tagline">Another Side of Life</div>
    </div>

        <a href="../../categories/">categories</a>
        <a href="../..">&larr; Home</a>
</nav><footer class="footer">
    <span>
        &copy; jpli &middot;
        under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC-BY-SA 4.0</a>
    </span>
    |
    <span>
        Powered by:
        <a href="http://blog.getpelican.com/">Pelican</a>
    </span>
</footer></body>
</html>